"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const global_1 = require("../../../config/global");
function registerFightNode(lf) {
    lf.register('fightNode', ({ RectNode, RectNodeModel, h }) => {
        class Node extends RectNode {
            getTitleElement() {
                const { model } = this.props;
                const { x, y } = model;
                const style = model.getNodeStyle();
                const properties = model.getProperties();
                if (properties.fightList) {
                    // 创建一个文本输入框
                    return h("g", {}, [
                        h('text', {
                            fill: '#000000',
                            fontSize: 16,
                            x: x - 60,
                            y: y - 10 - 20 * properties.fightList.length,
                            width: 50,
                            height: 25
                        }, 'CC_FightNode')
                    ]);
                }
            }
            ;
            getLocIconShape() {
                const { model } = this.props;
                const { x, y } = model;
                const { stroke } = model.getNodeStyle();
                const nodeData = model.getData();
                const properties = model.getProperties();
                if (properties.fightList) {
                    return h('g', {}, [
                        h("svg", {
                            x: x - 60,
                            y: y + 85 - 20 * properties.fightList.length,
                            width: 15,
                            height: 15,
                            viewBox: '0 0 1126 1024',
                        }, h('path', {
                            fill: stroke,
                            d: 'M801.171 483.589H544V226.418c0-17.673-14.327-32-32-32s-32 14.327-32 32v257.171H222.83c-17.673 0-32 14.327-32 32s14.327 32 32 32H480v257.17c0 17.673 14.327 32 32 32s32-14.327 32-32v-257.17h257.171c17.673 0 32-14.327 32-32s-14.327-32-32-32z'
                        })),
                        h("svg", {
                            x: x + 50,
                            y: y + 45,
                            width: 15,
                            height: 15,
                            viewBox: '0 0 1126 1024',
                        }, h('path', {
                            fill: stroke,
                            d: 'M801.171 547.589H222.83c-17.673 0-32-14.327-32-32s14.327-32 32-32h578.341c17.673 0 32 14.327 32 32s-14.327 32-32 32z'
                        })),
                    ]);
                }
            }
            getAllChoiceList() {
                const { model } = this.props;
                const { width, height, x, y, } = model;
                const style = model.getNodeStyle();
                const nodeData = model.getData();
                const properties = model.getProperties();
                if (properties.fightList) {
                    // 遍历choiceList并创建对应的h("rect")元素
                    const fightElements = nodeData.properties.fightList.map((choice, index) => {
                        // 在这里可以根据choice对象的属性来设置rect元素的属性
                        return h("foreignObject", Object.assign(Object.assign({}, style), { id: "fight", type: "fight", height: 26, width: 160, y: y + 20 - 20 * properties.fightList.length + 40 * index, x: x - 102 }), [
                            h("textarea", {
                                style: {
                                    width: "95%",
                                    height: "70%",
                                    resize: "none" // 禁止调整文本框大小
                                },
                                value: properties.fightList[index].fight_id,
                                onInput: (event) => {
                                    let newContent = event.target.value;
                                    properties.fightList[index].fight_id = newContent;
                                    model.setProperties(properties);
                                },
                            })
                        ]);
                    });
                    // 返回包含所有choice元素的SVG
                    return h("g", {}, fightElements);
                }
                return null; // 如果没有choice数据则返回null或其他默认值
            }
            getShape() {
                const { model } = this.props;
                const { width, height, x, y, } = model;
                const style = model.getNodeStyle();
                const properties = model.getProperties();
                if (properties.fightList) {
                    return h("g", {}, [
                        h("rect", Object.assign(Object.assign({}, style), { id: "svg_1", height: 60 + 40 * properties.fightList.length, width: 220, x: x - 220 / 2, y: y - 20 * properties.fightList.length, fill: "#fd4242" })),
                        h("rect", Object.assign(Object.assign({}, style), { id: "svg_7", height: 30, width: 220, x: x - 220 / 2, y: y - 30 - 20 * properties.fightList.length })),
                        this.getTitleElement(),
                        this.getAllChoiceList(),
                        this.getChangeList(),
                        // this.getLocIconShape(),
                    ]);
                }
            }
            getChangeList() {
                const { model } = this.props;
                const { width, height, x, y, } = model;
                const style = model.getNodeStyle();
                const textStyle = model.getTextStyle();
                const { stroke } = model.getNodeStyle();
                const nodeData = model.getData();
                const properties = model.getProperties();
                if (properties.fightList) {
                    return h("g", {}, [
                        h("rect", Object.assign(Object.assign({}, style), { id: "add", type: "add", height: 16, width: 100, y: y + 32 + 20 * properties.fightList.length, x: x - 102, fill: "#e8c9b5", onClick: (event) => {
                                const newContent = String.fromCharCode(65 + properties.fightList.length);
                                const newChoice = new global_1.choice(global_1.GraphRes.getNextId(), newContent);
                                nodeData.properties.fightList.push(newChoice);
                                // 获取当前的Model实例
                                nodeData.text.y += 20;
                                model.initNodeData(nodeData);
                                model.addField();
                            } })),
                        h("svg", {
                            x: x - 60,
                            y: y + 32 + 20 * properties.fightList.length,
                            width: 15,
                            height: 15,
                            viewBox: '0 0 1126 1024',
                        }, h('path', {
                            fill: stroke,
                            d: 'M801.171 483.589H544V226.418c0-17.673-14.327-32-32-32s-32 14.327-32 32v257.171H222.83c-17.673 0-32 14.327-32 32s14.327 32 32 32H480v257.17c0 17.673 14.327 32 32 32s32-14.327 32-32v-257.17h257.171c17.673 0 32-14.327 32-32s-14.327-32-32-32z'
                        })),
                        h("rect", Object.assign(Object.assign({}, style), { id: "substract", type: "substract", height: 16, width: 100, y: y + 32 + 20 * properties.fightList.length, x: x + 5, fill: "#e8c9b5", onClick: (event) => {
                                nodeData.properties.fightList.pop();
                                nodeData.text.y -= 20;
                                model.initNodeData(nodeData);
                                model.addField();
                            } })),
                        h("svg", {
                            x: x + 50,
                            y: y + 32 + 20 * properties.fightList.length,
                            width: 15,
                            height: 15,
                            viewBox: '0 0 1126 1024',
                        }, h('path', {
                            fill: stroke,
                            d: 'M801.171 547.589H222.83c-17.673 0-32-14.327-32-32s14.327-32 32-32h578.341c17.673 0 32 14.327 32 32s-14.327 32-32 32z'
                        })),
                    ]);
                }
            }
        }
        class Model extends RectNodeModel {
            constructor(data, graphModel) {
                data.text = {
                    value: (data.text && data.text.value) || '未命名节点-战斗',
                    x: data.x,
                    y: data.y + 100
                };
                super(data, graphModel);
                this.radius = 20;
            }
            initNodeData(data) {
                if (!data.virtual && !data.properties) {
                    const fight = new global_1.FightNode();
                    data.properties = Object.assign({}, fight);
                    this.width = 220;
                    this.height = 140;
                }
                super.initNodeData(data);
                if (data.properties && data.properties.fightList) {
                    this.width = 220;
                    this.height = 60 + 40 * data.properties.fightList.length;
                }
                const onlyPosAsConnect = {
                    message: "连接对应的锚点",
                    validate: (sourceNode, targetNode, sourceAnchor, targetAnchor) => {
                        return sourceAnchor.type === 'right' && targetAnchor.type === 'left';
                    },
                };
                const onlyAnchorsAsConnect = {
                    message: "只能连接一个锚点",
                    validate: (sourceNode, targetNode, sourceAnchor, targetAnchor) => {
                        const allEdges = sourceNode.outgoing.edges;
                        for (const key in allEdges) {
                            if (allEdges[key].sourceAnchorId == sourceAnchor.id) {
                                return false;
                            }
                        }
                        return true;
                    },
                };
                this.sourceRules.push(onlyPosAsConnect);
                this.sourceRules.push(onlyAnchorsAsConnect);
            }
            addField() {
                this.setAttributes();
                // 为了保持节点顶部位置不变，在节点变化后，对节点进行一个位移,位移距离为添加高度的一半。
                this.move(0, 24 / 2);
                // 更新节点连接边的path
                this.incoming.edges.forEach((egde) => {
                    // 调用自定义的更新方案
                    egde.updatePathByAnchor();
                });
                this.outgoing.edges.forEach((edge) => {
                    // 调用自定义的更新方案
                    edge.updatePathByAnchor();
                });
            }
            getAnchorStyle(anchorInfo) {
                const style = super.getAnchorStyle(anchorInfo);
                if (anchorInfo.type === "left") {
                    style.fill = "red";
                    style.hover.fill = "transparent";
                    style.hover.stroke = "transpanrent";
                    style.className = "lf-hide-default";
                }
                else if (anchorInfo.type === "right") {
                    if (anchorInfo.id == "win") {
                        style.fill = "green";
                        style.hover.fill = "transparent";
                        style.hover.stroke = "transpanrent";
                        style.className = "lf-hide-default";
                    }
                    else {
                        style.fill = "purple";
                        style.hover.fill = "transparent";
                        style.hover.stroke = "transpanrent";
                        style.className = "lf-hide-default";
                    }
                }
                return style;
            }
            setIsShowAnchor() {
                this.isShowAnchor = true;
            }
            getDefaultAnchor() {
                const { width, height, x, y, id } = this;
                return [{
                        x: x + 26 - width / 2,
                        y: y + 16 - height / 2,
                        type: "left",
                        edgeAddable: false,
                        id: `${id}_0`,
                    },
                    {
                        x: x - 28 + width / 2,
                        y: y + 50 - height / 2,
                        type: "right",
                        id: 'win',
                    },
                    {
                        x: x - 28 + width / 2,
                        y: y + 45 - height / 2 + 40 * this.properties.fightList.length,
                        type: "right",
                        id: 'fail',
                    },
                ];
            }
        }
        return {
            view: Node,
            model: Model
        };
    });
}
exports.default = registerFightNode;
